package src.main.java.com.health.coupon.utils;

import com.health.coupon.constant.Constants;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
@Slf4j
public class JwtTokenUtils {


    //生成token
    public static String generateToken(String phone, String id, Date expiration){
        JwtBuilder jwtBuilder = Jwts.builder();
        jwtBuilder.setId(id);
        jwtBuilder.setSubject(phone);
        jwtBuilder.setExpiration(expiration);
        jwtBuilder.signWith(SignatureAlgorithm.HS256, Constants.SECRECT);
        return jwtBuilder.compact();
    }

    //解密
    public static boolean parseToken(String token){
        try {
            Claims body = Jwts.parser().setSigningKey(Constants.SECRECT).
                    parseClaimsJws(token).getBody();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }



}
